import {
	BaseURL
} from '@/config/index.js'
import {
	useUserStore
} from '@/stores/modules/user.js'
import { store } from '@/stores/index.js'

export const get = (url, params, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// 头部添加Authorization
	options = addAuthorization(options)
	return uni.request({
		method: 'GET',
		url,
		data: params,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data
		} else {
			return Promise.reject(res)
		}
	}).then(res => {
		if (res.code === 200) {
			return res.data
		} else {
			if (!(options && options.hideErrorToast) && res.code !== 10001) {
				showError(res.message)
			}
			responseErrorHandle(res,options)
			return Promise.reject(res)
		}
	})
}

export const post = (url, data, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// 头部添加Authorization
	options = addAuthorization(options)
	return uni.request({
		method: 'POST',
		url,
		data,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data
		} else {
			responseErrorHandle(res,options)
			return Promise.reject(res)
		}
	}).then(res => {
		if (res.code === 200) {
			return res.data
		} else {
			if (!(options && options.hideErrorToast) && res.code !== 10001) {
				showError(res.message)
			}
			responseErrorHandle(res,options)
			return Promise.reject(res)
		}
	})
}

export const put = (url, data, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// 头部添加Authorization
	options = addAuthorization(options)
	return uni.request({
		method: 'PUT',
		url,
		data,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data
		} else {
			return Promise.reject(res)
		}
	}).then(res => {
		if (res.code === 200) {
			return res.data
		} else {
			if (!(options && options.hideErrorToast) && res.code !== 10001) {
				showError(res.message)
			}
			responseErrorHandle(res,options)
			return Promise.reject(res)
		}
	})
}

export const del = (url, data, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	// 头部添加Authorization
	options = addAuthorization(options)
	return uni.request({
		method: 'DELETE',
		url,
		data,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data
		} else {
			return Promise.reject(res)
		}
	}).then(res => {
		if (res.code === 200) {
			return res.data
		} else {
			if (!(options && options.hideErrorToast) && res.code !== 10001) {
				showError(res.message)
			}
			responseErrorHandle(res,options)
			return Promise.reject(res)
		}
	})
}

export const uploadProgress = (url, options,progressUpdate) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	const uploadTask = uni.uploadFile({
		url,
		...options
		// complete: (uploadFileRes) => {
		// 	if (uploadFileRes.statusCode === 200) {
		// 		return uploadFileRes.data && JSON.parse(uploadFileRes.data)
		// 	} else {
		// 		let data = uploadFileRes.data && JSON.parse(uploadFileRes.data)
		// 		if (!(options && options.hideErrorToast)) {
		// 			showError(data.message)
		// 		}
		// 		responseErrorHandle(data,options)
		// 		return Promise.reject(data)
		// 	}
		// }
	})
	if(progressUpdate){
		uploadTask.onProgressUpdate(progressUpdate);
	}
	return uploadTask
}
export const upload = (url, options) => {
	if (!isURL(url)) {
		url = BaseURL + url
	}
	return uni.uploadFile({
		url,
		...options
	}).then(res => {
		if (res.statusCode === 200) {
			return res.data && JSON.parse(res.data)
		} else {
			if (!(options && options.hideErrorToast) && res.code !== 10001) {
				showError(res.message)
			}
			responseErrorHandle(res,options)
			return Promise.reject(res)
		}
	})
}
const addAuthorization = (options) => {
	// 头部添加Authorization
	const token = uni.getStorageSync('token')
	if (token) {
		if (options) {
			if (options.header) {
				if (!options.header.Authorization) {
					options.header.Authorization = token
				}
			} else {
				options.header = {
					Authorization: token
				}
			}
		} else if (!options) {
			options = {
				header: {
					Authorization: token
				}
			}
		}
		return options
	}
	return options
}

const isURL = (url) => {
	return url && (url.startsWith('https://') || url.startsWith('http://'))
}

const showError = (msg) => {
	setTimeout(() => {
		msg && uni.showToast({
			icon: 'none',
			title: msg
		})
	}, 100)
}

const responseErrorHandle = (res,options) => {
	if (res.code === 10001) {
		const userStore = useUserStore(store)
		userStore.setToken(null)
		userStore.setUserInfo(null)
		let params = ''
		if(options && options.linkParamsUrl){
			params = `?${options.linkParamsUrl}`
		}
		const pages = getCurrentPages()
		if (pages && pages.length && pages[pages.length - 1].route === 'pagesA/login/login') {
			return
		}
		uni.navigateTo({
			url: `/pagesA/login/login${params}`,
			events: {
				onLoginSuccess() {
				}
			}
		})
	}
}